package com.wencai.test;

import com.wencai.bean.IndexStocks;
import com.alibaba.excel.util.StringUtils;
import com.wencai.util.IOUtils;

import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author wangpan
 * @date 2023/5/10
 */
public class Fix {
    private static String outDir = "D:\\Users\\Administrator\\Desktop\\记录\\概念\\";
    private static String inDir = "D:\\Users\\Administrator\\Desktop\\记录\\概念 - 副本\\";

    private static Pattern pattern = Pattern.compile("([^\\(\\)]+)\\(([.0-9]+)\\):(.*)");
    private static Pattern stockPattern = Pattern.compile("\\s*([a-zA-Z\u4e00-\u9fa5]+)\\s*([.0-9]++)\\s*");

    private static String outPath = "D:\\Users\\Administrator\\Desktop\\记录\\所有索引.txt";

    private static Set<String> set = new HashSet<>();


    public static void main(String[] args) throws IOException {
        Map<String, String> map = new HashMap<>();
        List<String> indexes = IOUtils.readFromFile(outPath, "gbk");

        for (String line : indexes) {
            if (line.startsWith("sz") || line.startsWith("sh")) {
                String[] split = line.split(",");
                map.put(split[2], split[0]);
            }
        }

        File file = new File(inDir);
        String[] list = file.list();
        for (String fileName : list) {
            if (!fileName.endsWith(".txt")) {
                continue;
            }
            List<String> list1 = IOUtils.readFromFile(inDir + fileName);
            int i = 0;
            StringBuilder builder = new StringBuilder();
            for (String line : list1) {
                IndexStocks indexStocks = new IndexStocks();
                Matcher matcher = pattern.matcher(line);
                matcher.matches();
                String indexName = matcher.group(1);
                String indexIncrement = matcher.group(2);
                indexStocks.setIndexName(indexName);
                indexStocks.setIndexIncrement(indexIncrement);
                String stocks = matcher.group(3).trim();
                indexStocks.setOrder(String.valueOf(++i));
                if (!StringUtils.isEmpty(indexName)) {
                    builder.append(indexName).append("(").append(indexIncrement).append("): ");
                }

                if (!stocks.matches("\\s+")) {
                    Matcher stockMatcher = stockPattern.matcher(stocks);
                    if (stockMatcher.find()) {
                        String name = stockMatcher.group(1);
                        indexStocks.setFirstStockName(name);
                        indexStocks.setFirstStockCode(getIdFromName(map, name));
                        indexStocks.setFirstStockIncrement(stockMatcher.group(2));
                        builder.append(indexStocks.getFirstStockName()).append("_").append(indexStocks.getFirstStockCode()).append(" ").append(indexStocks.getFirstStockIncrement());
                    }
                    if (stockMatcher.find()) {
                        String name = stockMatcher.group(1);
                        indexStocks.setSecondStockName(name);
                        indexStocks.setSecondStockCode(getIdFromName(map, name));
                        indexStocks.setSecondStockIncrement(stockMatcher.group(2));
                        builder.append(indexStocks.getSecondStockName()).append("_").append(indexStocks.getSecondStockCode()).append(" ").append(indexStocks.getSecondStockIncrement());
                    }
                    if (stockMatcher.find()) {
                        String name = stockMatcher.group(1);
                        indexStocks.setThirdStockName(name);
                        indexStocks.setThirdStockCode(getIdFromName(map, name));
                        indexStocks.setThirdStockIncrement(stockMatcher.group(2));
                        builder.append(indexStocks.getThirdStockName()).append("_").append(indexStocks.getThirdStockCode()).append(" ").append(indexStocks.getThirdStockIncrement());
                    }
                    builder.append(System.lineSeparator());
                }
            }
            IOUtils.writeFile(outDir + fileName, builder.toString());
        }
        System.out.println(set.size());
        System.out.println(set);
    }

    private static String getIdFromName(Map<String, String> map, String name) {
        if (!map.containsKey(name)) {
            set.add(name);
        }
        return map.get(name);
    }

}
